<?php

namespace App\Services\Home;

use App\Models\Attribute;
use App\Models\Handroom;
use App\Models\Home\Property;
use App\Models\Region;
use App\Models\Rentalhouse;

class MaproomService
{
    protected $handService;
    protected $attrService;

    public function __construct(HandroomService $handroomService, AttributeService $attributeService)
    {
        $this->handService = $handroomService;
        $this->attrService = $attributeService;
    }

    /**
     * @param $city_id
     * @return \Illuminate\Http\JsonResponse
     * 获取当前城市所包含的区域/县
     */
    public function getMaproomDataByCityId($request)
    {
        // 获取行政区
        if ($request['type'] == 'new'){
            $attr = Region::with('property')->where('parent_id', $request['city_id'])->where('status', 'T')->get(['id', 'name', 'longitude', 'latitude']);
        }elseif ($request['type'] == 'hand'){
            $attr = Region::with('handroom')->where('parent_id', $request['city_id'])->where('status', 'T')->get(['id', 'name', 'longitude', 'latitude']);
        }elseif ($request['type'] == 'rental'){
            $attr = Region::with('rentalhouse')->where('parent_id', $request['city_id'])->where('status', 'T')->get(['id', 'name', 'longitude', 'latitude']);
        }

        foreach ($attr as $k => $v) {
            if ($request['type'] == 'new'){
                $attr[$k]['count'] = count($v->property);
            }elseif ($request['type'] == 'hand'){
                $attr[$k]['count'] = count($v->handroom);
            }elseif ($request['type'] == 'rental'){
                $attr[$k]['count'] = count($v->rentalhouse);
            }
        }
        return response()->json([
            'error' => 0,
            'area' => $attr
        ]);
    }

    public function getMaproomAllDataByCityId($request)
    {
        if($request['type'] == 'new'){
            $model = Property::query();
            $model = $this->getNewProperty($request,$model);
        }elseif ($request['type'] == 'hand'){
            $model = Handroom::query();
            $model = $this->getHandroom($request,$model);
        }elseif ($request['type'] == 'rental'){
            $model = Rentalhouse::query();
            $model = $this->getRentalhouse($request,$model);
        }

        if ($request['area_id'] > 0) {
            $model = $model->where('area', $request['area_id']);
        } elseif ($request['city_id'] > 0 && $request['area_id'] == 0) {
            $model = $model->where('region', $request['city_id']);
        }
        if ($request['type'] == 'new'){
            $data = $model->where('status', 'T')->get(['id', 'title', 'longitude', 'latitude', 'average', 'thumb', 'average_unit']);
            $scroll = $model->with(['apartments'])->where('status', 'T')->paginate(10);
        }elseif ($request['type'] == 'hand' || $request['type'] == 'rental'){
            $data = $model->where('status', 'T')->get(['id', 'title', 'longitude', 'latitude','price', 'acreage', 'thumb', 'communityname','year']);
            $scroll = $model->with(['category'])->where('status', 'T')->paginate(10);
        }

        if (count($data) && count($scroll)) {
            return response()->json([
                'error' => 0,
                'data' => $data,
                'scroll' => $scroll
            ]);
        } elseif(count($data) && !count($scroll)) {
            return response()->json([
                'error' => 1,
                'data' => $data,
                'message' => '没有更多了'
            ]);
        } else {
        return response()->json([
            'error' => 1,
            'data' => [],
            'message' => '没有更多了'
        ]);
    }
    }

//    public function getMaproomScrollData($request)
//    {
//        if ($request['type'] == 'new') {
//            return $this->getNewProperty($request);
//        } elseif ($request['type'] == 'hand') {
//            return $this->getHandroom($request);
//        } elseif ($request['type'] == 'rental') {
//            return $this->getRentalhouse($request);
//        }
//    }

    public function getNewProperty($request,$model)
    {
        if (!empty($request['apartname'])) {
            $model = $model->where('title', 'like', '%' . $request['apartname'] . '%');
        }
        if ($request['average']) {
            $price = explode(',', $request['average']);
            $model = $model->whereBetween('average', [$price[0], $price[1]]);
        }
        if ($request['wytype']){
            $model = $model->where('wytype', $request['wytype']);
        }
        if ($request['sale_status']){
            $model = $model->where('salestatus', $request['sale_status']);
        }
        if ($request['sort'] == 'default') {
            $model = $model->orderBy('id', 'desc');
        } elseif ($request['sort'] == 'down_price') {
            $model = $model->orderBy('average', 'asc');
        } elseif ($request['sort'] == 'up_price') {
            $model = $model->orderBy('average', 'desc');
        } elseif ($request['sort'] == 'down_time') {
            $model = $model->orderBy('opentime', 'asc');
        } elseif ($request['sort'] == 'up_time') {
            $model = $model->orderBy('opentime', 'desc');
        }
        return $model;
    }

    public function getHandroom($request,$model)
    {
        if (!empty($request['apartname'])) {
            $model = $model->where('communityname', 'like', '%' . $request['apartname'] . '%');
        }
        if ($request['hand_price']) {
            $price = explode(',', $request['hand_price']);
            $model = $model->whereBetween('price', [$price[0], $price[1]]);
        }
        if ($request['hand_acreage']) {
            $acreage = explode(',', $request['hand_acreage']);
            $model = $model->whereBetween('acreage', [$acreage[0], $acreage[1]]);
        }
        if (!empty($request['hand_room'])) {
            if($request['hand_room'] == 5){
                $model = $model->where('room','>=', $request['hand_room']);
            }else{
                $model = $model->where('room', $request['hand_room']);
            }
        }
        if ($request['hand_age']) {
            $age = explode(',', $request['hand_age']);
            $min = date('Y') - $age[1];
            $max = date('Y') - $age[0];
            $model = $model->whereBetween('year', [$min, $max]);
        }
        if (!empty($request['hand_direction'])) {
            // 朝向
            $model = $model->where('category_id', $request['hand_direction']);
        }
        if (!empty($request['hand_floor'])) {
            $model = $model->where('floor', $request['hand_floor']);
        }
        if (!empty($request['hand_renovation'])) {
            $model = $model->where('renovation', $request['hand_renovation']);
        }
        if (!empty($request['hand_tags'])) {
//            $tags = implode(',',$request['hand_tags']);
//            $model = $model->where('tags','like','%'.$tags.'%');
            $ids = $this->handService->getHandroomByTagsId($request['hand_tags']);
            $model = $model->whereIn('id',$ids);
        }
        if ($request['sort'] == 'default') {
            $model = $model->orderBy('id', 'desc');
        } elseif ($request['sort'] == 'down_price') {
            $model = $model->orderBy('price', 'asc');
        } elseif ($request['sort'] == 'up_price') {
            $model = $model->orderBy('price', 'desc');
        } elseif ($request['sort'] == 'down_time') {
            $model = $model->orderBy('acreage', 'asc');
        } elseif ($request['sort'] == 'up_time') {
            $model = $model->orderBy('acreage', 'desc');
        }

        return $model;
    }

    public function getRentalhouse($request, $model)
    {
        if (!empty($request['apartname'])) {
            $model = $model->where('communityname', 'like', '%' . $request['apartname'] . '%');
        }
        if ($request['rental_price']) {
            $price = explode(',', $request['rental_price']);
            $model = $model->whereBetween('price', [$price[0], $price[1]]);
        }
        if ($request['rental_acreage']) {
            $acreage = explode(',', $request['rental_acreage']);
            $model = $model->whereBetween('acreage', [$acreage[0], $acreage[1]]);
        }
        if (!empty($request['rental_room'])) {
            if($request['rental_room'] == 5){
                $model = $model->where('room','>=', $request['rental_room']);
            }else{
                $model = $model->where('room', $request['rental_room']);
            }
        }

        if (!empty($request['rental_direction'])) {
            // 朝向
            $model = $model->where('category_id', $request['rental_direction']);
        }
        if (!empty($request['rental_floor'])) {
            $model = $model->where('floor', $request['rental_floor']);
        }
        if (!empty($request['rental_renovation'])) {
            $model = $model->where('renovation', $request['rental_renovation']);
        }
        if (!empty($request['rental_type'])) {
            $model = $model->where('rental_type',$request['rental_type']);
        }
        if ($request['sort'] == 'default') {
            $model = $model->orderBy('id', 'desc');
        } elseif ($request['sort'] == 'down_price') {
            $model = $model->orderBy('price', 'asc');
        } elseif ($request['sort'] == 'up_price') {
            $model = $model->orderBy('price', 'desc');
        } elseif ($request['sort'] == 'down_time') {
            $model = $model->orderBy('acreage', 'asc');
        } elseif ($request['sort'] == 'up_time') {
            $model = $model->orderBy('acreage', 'desc');
        }

        return $model;
    }

    public function initAreaByCityId($city_id)
    {
        // 物业类型
        $pmc = $this->attrService->getAttributeById(42);
        //朝向
        $direction =  $this->attrService->getAttributeById(84);
        //装修情况
        $renovation = $this->attrService->getAttributeById(47);
        //二手房特色标签
        $tags = $this->attrService->getAttributeById(74);
        if (($city_id)) {
            $city = Region::with('childs')->findOrFail($city_id, ['id', 'name']);
            return response()->json([
                'error' => 0,
                'city' => $city,
                'type' => $pmc,
                'direction' => $direction,
                'renovation' => $renovation,
                'tags' => $tags
            ]);
        } else {
            return response()->json([
                'error' => 1,
                'message' => '区域不存在'
            ]);
        }
    }

}